{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorBoard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "After Training the model, run \n",
    "\n",
    "    tensorboard --logdir=path/to/log-directory\n",
    "    \n",
    "Tensorboard provides a good visualization tool for all the variables you like and works on a browser."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def init_weights(shape, name):\n",
    "    return tf.Variable(tf.random_normal(shape, stddev=0.01), name=name)\n",
    "\n",
    "# This network is the same as the previous one except with an extra hidden layer + dropout\n",
    "def model(X, w_h, w_h2, w_o, p_keep_input, p_keep_hidden):\n",
    "    # Add layer name scopes for better graph visualization\n",
    "    with tf.name_scope(\"layer1\"):\n",
    "        X = tf.nn.dropout(X, p_keep_input)\n",
    "        h = tf.nn.relu(tf.matmul(X, w_h))\n",
    "    with tf.name_scope(\"layer2\"):\n",
    "        h = tf.nn.dropout(h, p_keep_hidden)\n",
    "        h2 = tf.nn.relu(tf.matmul(h, w_h2))\n",
    "    with tf.name_scope(\"layer3\"):\n",
    "        h2 = tf.nn.dropout(h2, p_keep_hidden)\n",
    "        return tf.matmul(h2, w_o)\n",
    "\n",
    "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n",
    "trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "X = tf.placeholder(\"float\", [None, 784], name=\"X\")\n",
    "Y = tf.placeholder(\"float\", [None, 10], name=\"Y\")\n",
    "\n",
    "w_h = init_weights([784, 625], \"w_h\")\n",
    "w_h2 = init_weights([625, 625], \"w_h2\")\n",
    "w_o = init_weights([625, 10], \"w_o\")\n",
    "\n",
    "# Add histogram summaries for weights\n",
    "tf.summary.histogram(\"w_h_summ\", w_h)\n",
    "tf.summary.histogram(\"w_h2_summ\", w_h2)\n",
    "tf.summary.histogram(\"w_o_summ\", w_o)\n",
    "\n",
    "p_keep_input = tf.placeholder(\"float\", name=\"p_keep_input\")\n",
    "p_keep_hidden = tf.placeholder(\"float\", name=\"p_keep_hidden\")\n",
    "py_x = model(X, w_h, w_h2, w_o, p_keep_input, p_keep_hidden)\n",
    "\n",
    "with tf.name_scope(\"cost\"):\n",
    "    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))\n",
    "    train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)\n",
    "    # Add scalar summary for cost\n",
    "    tf.summary.scalar(\"cost\", cost)\n",
    "\n",
    "with tf.name_scope(\"accuracy\"):\n",
    "    correct_pred = tf.equal(tf.argmax(Y, 1), tf.argmax(py_x, 1)) # Count correct predictions\n",
    "    acc_op = tf.reduce_mean(tf.cast(correct_pred, \"float\")) # Cast boolean to float to average\n",
    "    # Add scalar summary for accuracy\n",
    "    tf.summary.scalar(\"accuracy\", acc_op)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "with tf.Session() as sess:\n",
    "    # create a log writer. run 'tensorboard --logdir=./logs/nn_logs'\n",
    "    writer = tf.summary.FileWriter(\"./logs/nn_logs\", sess.graph) # for 1.0\n",
    "    merged = tf.summary.merge_all()\n",
    "\n",
    "    # you need to initialize all variables\n",
    "    tf.global_variables_initializer().run()\n",
    "\n",
    "    for i in range(100):\n",
    "        for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):\n",
    "            sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end],\n",
    "                                          p_keep_input: 0.8, p_keep_hidden: 0.5})\n",
    "        summary, acc = sess.run([merged, acc_op], feed_dict={X: teX, Y: teY,\n",
    "                                          p_keep_input: 1.0, p_keep_hidden: 1.0})\n",
    "        writer.add_summary(summary, i)  # Write summary\n",
    "        print(i, acc)                   # Report the accuracy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
